#ifndef WEB_PAGE_SERVER_HPP
#define WEB_PAGE_SERVER_HPP

#include <Arduino.h>
#include <FS.h>
#include <LittleFS.h>
#include <WiFiClient.h>
#include <WebServer.h>
#include "shared_values.h"




// 电机启动控制
void handleStartRequest(float speed) {
    if (stepper) {
        stepper->motorSpeed(speed); // 设置目标速度
        Serial.printf("[UMEKO][HTTP] Motor started with speed: %.2f\n", speed);
    } else {
        Serial.println("[UMEKO][HTTP] Stepper motor not initialized");
    }
}

// 电机停止控制
void handleStopRequest() {
    if (stepper) {
        stepper->motorSpeed(0); // 停止电机
        Serial.println("[UMEKO][HTTP] Motor stopped");
    } else {
        Serial.println("[UMEKO][HTTP] Stepper motor not initialized");
    }
}


// 初始化文件系统
void initFileSystem() {
    if (!LittleFS.begin()) {
        Serial.println("An error has occurred while mounting LittleFS");
    } else {
        Serial.println("LittleFS mounted successfully");
    }
}

// 设置服务器以提供 index.html
void setupWebServer() {
    server->on("/", HTTP_GET, []() {
        File file = LittleFS.open("/index.html", "r");
        if (!file) {
            server->send(500, "text/plain", "Failed to open index.html");
            return;
        }
        server->streamFile(file, "text/html");
        file.close();
    });

    server->on("/start", HTTP_GET, []() {
        if (server->hasArg("speed")) {
            float speed = server->arg("speed").toFloat();
            handleStartRequest(speed);
        }
        server->send(200, "text/plain", "Motor started");
    });

    server->on("/stop", HTTP_GET, []() {
        handleStopRequest();
        server->send(200, "text/plain", "Motor stopped");
    });

    server->on("/macro/start", HTTP_GET, []() {
        start_macro();
        server->send(200, "text/plain", "Macro started");
    });

    server->on("/macro/stop", HTTP_GET, []() {
        stop_macro();
        server->send(200, "text/plain", "Macro stopped");
    });

    server->on("/macro/content", HTTP_GET, []() {
        if (!LittleFS.exists(macro_path)) {
            server->send(404, "text/plain", "Macro file not found");
            return;
        }
        File file = LittleFS.open(macro_path, "r");
        if (!file) {
            server->send(500, "text/plain", "Failed to open macro file");
            return;
        }
        String content = file.readString();
        file.close();
        server->send(200, "text/plain", content);
    });

    // 保存宏文件
    server->on("/macro/save", HTTP_POST, []() {
        if (!server->hasArg("plain")) {
            server->send(400, "text/plain", "No content");
            return;
        }
        String content = server->arg("plain");
        File file = LittleFS.open(macro_path, "w");
        if (!file) {
            server->send(500, "text/plain", "Failed to open file for writing");
            return;
        }
        file.print(content);
        file.close();
        load_macro(); // 重新加载宏
        server->send(200, "text/plain", "Macro saved");
    });

    // 获取电机速度
    server->on("/motor/speed", HTTP_GET, []() {
        String json = "{\"speed\":" + String(stepper->freqSpeed, 2) + "}";
        server->send(200, "application/json", json);
    });

    server->begin();
    Serial.println("[UMEKO][HTTP] Web server started");
}

void task_handler(void *pvParameters) {
    for (;;) {
        server->handleClient(); // 处理 HTTP 请求
        delay(5);
    }
}

void startWebServerTask() {
    xTaskCreate(task_handler, "WebServerTask", 8192, NULL, 1, NULL);
    Serial.println("[UMEKO][HTTP] Web server task started");
}

#endif // WEB_PAGE_SERVER_HPP
